﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="JavaScriptSample._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to call CSharp function in Ajax | JavaScript Sample</title>

    <script type="text/javascript">
        function getServerTime() {
            /// <summary>
            /// Call ServerTime web method via ajax request
            /// </summary>
            if (window.XMLHttpRequest) {
                // for IE7+, Firefox, Chrome, Opera, Safari
                this.xmlhttp = new XMLHttpRequest();
            }
            else {
                // for IE6, IE5
                try {
                    this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e1) {
                    try {
                        // older version of Msxml
                        this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e2) {
                        this.xmlhttp = null;
                    }
                }
            }
            xmlhttp.onreadystatechange = function() {
                /// <summary>
                /// Display server time when success
                /// </summary>
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    // success Status
                    document.getElementById("ServerTimeResponse").innerHTML = xmlhttp.responseText;
                }
            }
            this.xmlhttp.open("POST", "AjaxServer.asmx/ServerTime", true);
            this.xmlhttp.send();
        }
    </script>

</head>
<body>
    <form id="ServerTimeForm" runat="server">
    <div>
        <input id="ServerTimeButton" type="button" onclick="getServerTime();" value="Get Server Time" />
        <div id="ServerTimeResponse" runat="server">
        </div>
    </div>
    </form>
</body>
</html>
